package com.away.transaction;

import java.util.Map;
import java.util.Random;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 模拟一个数据库用来放key_value
 */
public class OrderDB {
    public static Map<String,String> orders = new ConcurrentHashMap();

    /**
     * 插入订单信息，当orderId存在时 不插入
     * @param order
     * @param info
     * @return
     */
    public static boolean save(String order,String info){
        Random rand = new Random();
        try {
            Thread.sleep(rand.nextInt(1000));
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        String s = orders.putIfAbsent(order, info);
        if(s == null){
            return true;
        }
        else return false;
    }


    public static boolean exist(String orderId){
        return orders.containsKey(orderId);
    }
}
